package com.sayimo.maker.user.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.sayimo.framework.common.util.VerifyCodeUtil;
import com.sayimo.framework.exception.DaoException;
import com.sayimo.framework.exception.ServiceException;
import com.sayimo.maker.commons.Constant;
import com.sayimo.maker.commons.MessageVo;
import com.sayimo.maker.commons.ali.sms.AliSms;
import com.sayimo.maker.user.dao.SmsVerifyCodeInterfaceDao;
import com.sayimo.maker.user.service.SmsVerifyCodeInterfaceService;
import com.sayimo.maker.user.vo.SmsVerifyCode;

/**
 * @Project: sayimoWeixinWeb
 * @ClassName: SmsVerifyCodeInterfaceImplService
 * @Description: 短信验证码Service
 * @Author: 王瑞清
 * @Date: 日期：2015年8月18日 时间：下午3:42:04
 * @Version 1.0.0
 */
@Service(value = "smsVerifyCodeInterfaceImplService")
public class SmsVerifyCodeInterfaceImplService implements SmsVerifyCodeInterfaceService {

	private static Log logger = LogFactory.getLog(SmsVerifyCodeInterfaceImplService.class);
	@Autowired
	private SmsVerifyCodeInterfaceDao smsVerifyCodeInterfaceDao;

	public SmsVerifyCodeInterfaceDao getSmsVerifyCodeInterfaceDao() {
		return smsVerifyCodeInterfaceDao;
	}

	public void setSmsVerifyCodeInterfaceDao(
			SmsVerifyCodeInterfaceDao smsVerifyCodeInterfaceDao) {
		this.smsVerifyCodeInterfaceDao = smsVerifyCodeInterfaceDao;
	}
	
	/**
	 * 
	 * @MethodName sendSms
	 * @Description 发送短信验证码
	 * 
	 * @author chenpy
	 * @date 2015年12月25日 上午9:44:59
	 * @param 
	 * @return Map
	 * @VERSION 1.0.0
	 */
	@SuppressWarnings({ "rawtypes", "unchecked", "serial" })
	public Map sendSms(HttpServletRequest request) throws ServiceException{
		Map resultMap = new HashMap();
		SmsVerifyCode paramSmsCode = new SmsVerifyCode();
		paramSmsCode.setMobilePhone(request.getParameter("phoneNum"));
		paramSmsCode.setActionName(request.getParameter("busCode"));
		SmsVerifyCode verifyCode = querySmsVerifyCodeByCond(paramSmsCode);
		
		String smsCode = VerifyCodeUtil.getNumberVerifyCode(4);
		MessageVo messageVo = null;
		paramSmsCode.setVerifyCode(smsCode);
		if (verifyCode == null) {
			messageVo = insertVerifyCode(paramSmsCode);
		}else{
			if (verifyCode.getLoseTime().getTime() > (new Date()).getTime()&&verifyCode.getStatus()==1) { // 验证码存在且没有过期
				resultMap.put(Constant.STATUS, Constant.STATUS_SUCCESS);
				resultMap.put(Constant.ERROR_CODE, Constant.VERIFY_CODE_AVIABLE);
				resultMap.put(Constant.DATA, new HashMap(){{
					put(Constant.MSG, "发送成功");
				}});
				{
					AliSms.send(request.getParameter("phoneNum"), verifyCode.getVerifyCode());
				}
				return resultMap;
			}
			
			paramSmsCode.setId(verifyCode.getId());
			messageVo = updateVerifyCode(paramSmsCode);
		}

		if (Constant.STATUS_SUCCESS.equals(messageVo.getCode())) {
			AliSms.send(request.getParameter("phoneNum"), smsCode);
			resultMap.put(Constant.STATUS, Constant.STATUS_SUCCESS);
			resultMap.put(Constant.DATA, new HashMap(){{
				put(Constant.MSG, "发送成功");
			}});
		}else{
			resultMap.put(Constant.STATUS, Constant.STATUS_ERROR);
			resultMap.put(Constant.ERROR_CODE, "发送失败");
		}
		
		return resultMap;
	}

	/* (non-Javadoc)
	 * @see com.sayimo.school.mall.module.admin.service.SmsVerifyCodeInterfaceService#insertVerifyCode(com.sayimo.school.mall.module.admin.model.SmsVerifyCode)
	 */
	public MessageVo insertVerifyCode(SmsVerifyCode code) throws ServiceException {
		MessageVo messageVo = new MessageVo();
		try {
			int result = smsVerifyCodeInterfaceDao.insertVerifyCode(code);
			if (result > 0) {
				messageVo.setCode(Constant.STATUS_SUCCESS);
				messageVo.setMsg("发送成功");
				return messageVo;
			}
			messageVo.setCode(Constant.STATUS_ERROR);
			messageVo.setMsg("发送失败");
			return messageVo;
		} catch (DaoException e) {
			logger.error(e);
			throw new ServiceException(e);
		}
	}

	/* (non-Javadoc)
	 * @see com.sayimo.school.mall.module.admin.service.SmsVerifyCodeInterfaceService#updateVerifyCode(com.sayimo.school.mall.module.admin.model.SmsVerifyCode)
	 */
	public MessageVo updateVerifyCode(SmsVerifyCode code) throws ServiceException {
		MessageVo messageVo = new MessageVo();
		try {
			int result = smsVerifyCodeInterfaceDao.updateVerifyCode(code);
			if (result > 0) {
				messageVo.setCode(Constant.STATUS_SUCCESS);
				messageVo.setMsg("发送成功");
				return messageVo;
			}
			messageVo.setCode(Constant.STATUS_ERROR);
			messageVo.setMsg("发送失败");
			return messageVo;
		} catch (DaoException e) {
			logger.error(e);
			throw new ServiceException(e);
		}
	}

	/* (non-Javadoc)
	 * @see com.sayimo.school.mall.module.admin.service.SmsVerifyCodeInterfaceService#querySmsVerifyCodeByCond(com.sayimo.school.mall.module.admin.model.SmsVerifyCode)
	 */
	public SmsVerifyCode querySmsVerifyCodeByCond(SmsVerifyCode code) throws ServiceException {
		try {
			return this.smsVerifyCodeInterfaceDao.querySmsVerifyCodeByCond(code);
		} catch (DaoException e) {
			logger.error(e);
			throw new ServiceException(e);
		}
	}

	@SuppressWarnings({ "unchecked", "rawtypes", "serial"})
	public Map checkSmsCode(HttpServletRequest request) throws ServiceException{
		Map resultMap = new HashMap();
		try {
			// 验证短信验证码是否正确
			SmsVerifyCode paramVerifyCode = new SmsVerifyCode();
			paramVerifyCode.setMobilePhone(request.getParameter("mobilePhone"));
			paramVerifyCode.setActionName(request.getParameter("busCode"));
			paramVerifyCode.setVerifyCode(request.getParameter("smsCode"));
			SmsVerifyCode code = smsVerifyCodeInterfaceDao.querySmsVerifyCodeByCond(paramVerifyCode);
		
			if (code == null) { // 短信验证码为空或者没有给该手机发送过短信验证码
				resultMap.put(Constant.STATUS, Constant.STATUS_ERROR);
				resultMap.put(Constant.ERROR_CODE, Constant.VERIFY_CODE_NOT_EXISTS);
				return resultMap;
			}
			if (code.getLoseTime().getTime() < (new Date()).getTime()) { // 短信验证码已过期
				resultMap.put(Constant.STATUS, Constant.STATUS_ERROR);
				resultMap.put(Constant.ERROR_CODE, Constant.VERIFY_CODE_BEYOND_LOSETIME);
				return resultMap;
			}
			if(!code.getVerifyCode().equals(request.getParameter("smsCode"))){ // 验证码不正确
				resultMap.put(Constant.STATUS, Constant.STATUS_ERROR);
				resultMap.put(Constant.ERROR_CODE, Constant.VERIFY_CODE_ERROR);
				return resultMap;
			}
			resultMap.put(Constant.STATUS, Constant.STATUS_SUCCESS);
			resultMap.put(Constant.DATA, new HashMap(){{
				put(Constant.MSG, "验证成功");
			}});
			//非注册验证时
			if(!"002".equals(paramVerifyCode.getActionName())){
				//验证成功后将验证码失效
				smsVerifyCodeInterfaceDao.updateSmsVerifyCodeByCond(paramVerifyCode);
			}
			
		} catch (DaoException e) {
			logger.error(e);
			throw new ServiceException(e);
		}
		return resultMap;
	}
}
